<?php
/**
 * 熵巡 - 企业级氚云OpenAPI数据管理平台
 * 开发归属 Alyr.net
 * 获取表单列表接口 - 读取所有已配置表单
 * 尊重劳动成果是一种美德
 */

error_reporting(0);
ini_set('display_errors', 0);

header('Content-Type: application/json; charset=utf-8');

try {
    $configFile = __DIR__ . '/../config.php';
    
    if (!file_exists($configFile)) {
        echo json_encode([
            'success' => false,
            'message' => '配置文件不存在'
        ]);
        exit;
    }
    
    $config = require $configFile;
    
    // 获取schemas配置
    $schemas = $config['schemas'] ?? [];
    
    // 确保default表单存在
    if (!isset($schemas['default'])) {
        $schemas['default'] = [
            'schema_key' => 'default',
            'schema_name' => '默认表单',
            'schema_code' => $config['schema_code'] ?? '',
            'schema_desc' => '主表单',
            'image_fields' => $config['image_fields'] ?? [],
            'attachment_fields' => $config['attachment_fields'] ?? [],
            'token_id' => '',
            'token_secret' => ''
        ];
    }
    
    // 转换为数组格式
    $schemaList = [];
    foreach ($schemas as $key => $schema) {
        $schema['schema_key'] = $key;
        $schemaList[] = $schema;
    }
    
    echo json_encode([
        'success' => true,
        'schemas' => $schemaList
    ]);
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage()
    ]);
}
